<?php
namespace App\Http\Controllers\Admin;

use App\Utilitys\BaiduyunOcr;
use App\Service\Admin\MakerService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class MakerController extends BaseController
{
    //首页

    public function __construct(MakerService $service)
    {
        $this->db = $service;
    }

    //选项
    public function getOption(Request $request)
    {
        //header('Access-Control-Allow-Origin: *');
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $res = $this->db->getOption();
        return $this->toJson(0, '', $res['data']);
    }

    //创建项目
    public function itemAdd(Request $request)
    {
        //header('Access-Control-Allow-Origin: *');
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $pas['type'] = $request->post('item', 0);
        $pas['front'] = $request->post('front', 0);
        $pas['back'] = $request->post('back', 0);
        $pas['font'] = $request->post('font', 0);
        $pas['cn'] = $request->post('title', '');
        if (!$pas['type']) {
            return $this->toJson(-2, '项目类型必须的');
        }
        if ($pas['type'] != 1) {
            return $this->toJson(-2, '项目类型目前只能是网站');
        }
        if (!$pas['front']) {
            return $this->toJson(-2, '前端技术栈必须的');
        }
        if ($pas['front'] != 4) {
            return $this->toJson(-2, '前端技术栈目前只能原生的');
        }
        if (!$pas['back']) {
            return $this->toJson(-2, '后端技术栈必须的');
        }
        if ($pas['back'] != 10) {
            return $this->toJson(-2, '后端技术栈目前只能Laravel系的');
        }
        if (!$pas['font']) {
            return $this->toJson(-2, '字体必须的');
        }
        if (!$pas['cn']) {
            return $this->toJson(-2, '项目名称必须的');
        }
        $res = $this->db->addItem($uid, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //上传图片
    public function upload(Request $request)
    {
        //header('Access-Control-Allow-Origin: *');
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $item_id = $request->post('item_id', '');
        $itemOne = $this->db->itemOne($item_id);
        if (empty($itemOne)) {
            return $this->toJson(-2, '没有项目');
        }
        //上传
        $resUpload = Upload::uploadImg($request, 'image');
        if ($resUpload['code'] < 0) {
            return $this->toJson(-2, $resUpload['msg']);
        }
        //入库
        $imglink = $resUpload['data'];
        $res = $this->db->addImg($token, $imglink);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $resData = [
            'img_id' => $res['data'],
            'imglink' => env('DOMAIN') . '/' . $imglink,
        ];
        return $this->toJson(0, '', $resData);
    }

    /**
     * 文字识别
     */
    public function wordByAi(Request $request)
    {
        //header('Access-Control-Allow-Origin: *');
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $item_id = $request->post('item_id', '');
        $itemOne = $this->db->itemOne($item_id);
        if (empty($itemOne)) {
            return $this->toJson(-2, '没有项目');
        }
        $imgid = $request->post('img_id', 0);
        $imgOne = $this->db->getImgOne($imgid);
        if ($imgOne['code'] < 0) {
            return $this->toJson(-2, $imgOne['msg']);
        }
        $imglink = $imgOne['data']['link'];
        //获取鉴权Access_token
        $res = BaiduyunOcr::getAccessToken();
        if ($res['code'] < 0) {
            return $this->toJson(-2, '鉴权失败', $res['data']);
        }
        $access_token = $res['data']['access_token'];
        //通用文字标准版
        $res = BaiduyunOcr::getTextFromImg($access_token, $imglink);
        if ($res['code'] < 0) {
            return $this->toJson(-2, '调用失败', $res['data']);
        }
        $words = $res['data']['words_result'];
        if (empty($words)) {
            return $this->toJson(-2, '没有文字信息');
        }
        //保存文字信息
        $res = $this->db->modifyImg($imgid, $words);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    /**
     * 匹配识别结果和库信息
     */
    public function mate(Request $request)
    {
        //header('Access-Control-Allow-Origin: *');
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $item_id = $request->post('item_id', '');
        $itemOne = $this->db->itemOne($item_id);
        if (empty($itemOne)) {
            return $this->toJson(-2, '没有项目');
        }
        $words = $request->post('words', '');
        if (!$words) {
            return $this->toJson(-2, '没有词汇');
        }
        $words = explode('||||', $words);
        $res = $this->db->addWord($uid, $item_id, $words);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //生成前端页面
    public function mkFront(Request $request)
    {
        //header('Access-Control-Allow-Origin: *');
        $token = $request->post('token', '');
        $uid = $this->db->getUidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $item_id = $request->post('item_id', '');
        $funid = $request->post('fun_id', 0);
        $itemOne = $this->db->itemOne($item_id);
        if (empty($itemOne)) {
            return $this->toJson(-2, '没有项目');
        }
        $res = $this->db->mkFront($uid, $item_id, $funid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }
}